package com.baicaoyuan.web.service;

import com.baicaoyuan.web.common.TimeUtils;
import com.baicaoyuan.web.enums.LevelEnum;
import com.baicaoyuan.web.model.ProjectItemUserDO;
import com.baicaoyuan.web.model.ScoreDetailDO;
import com.baicaoyuan.web.repository.ScoreDetailRepository;
import com.baicaoyuan.web.response.ScoreDetailResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 功能描述：
 *
 * @author 罗富祥
 * 创建时间： 2018/1/4
 */
@Service
public class ScoreDetailService {

    @Autowired
    private ScoreDetailRepository scoreDetailRepository;

    public void save(Long userId, String userName, BigDecimal score, ProjectItemUserDO projectItemUser, LevelEnum level) {
        ScoreDetailDO sd = new ScoreDetailDO();
        sd.setUserId(userId);
        sd.setUserName(userName);
        sd.setScore(score);
        if (projectItemUser != null) {
            sd.setProjectBusinessName(projectItemUser.getProjectItemName());
            sd.setProjectItemId(projectItemUser.getProjectItemId());
            sd.setApplyUserId(projectItemUser.getUserId());
            sd.setApplyUserName(projectItemUser.getUserName());
            sd.setApplyUserTelephone(projectItemUser.getTelephone());
        }
        sd.setLevel(level.getStatus());
        sd.setCreateTime(TimeUtils.getCurrentTime());
        Objects.requireNonNull(this.scoreDetailRepository.save(sd));
    }

    public List<ScoreDetailResponse> getScoreDetails(Long userId) {
        List<ScoreDetailResponse> result = new ArrayList<>();
        List<ScoreDetailDO> list = this.scoreDetailRepository.findByUserIdOrderByCreateTime(userId);
        list.stream().forEach(x -> {
            ScoreDetailResponse res = new ScoreDetailResponse();
            BeanUtils.copyProperties(x, res);
            result.add(res);
        });

        return result;
    }
}

